package com.zl.smartmall.library.listener;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zl.smartmall.library.po.DictDistrictInfo;

public abstract class GetDictDistrictListener extends BaseListener {


	@Override
	public void onOffline() {
	}

	public abstract void onResponseSuccess(List<DictDistrictInfo> lists);
	
	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
		try {
			int ret = response.getInt("ret");
			switch (ret) {
			case 0:
				JSONObject result = response.getJSONObject("result");
				JSONArray clists = result.getJSONArray("list");
				List<DictDistrictInfo> lists = new ArrayList<DictDistrictInfo>();
				for(int i = 0;i<clists.length();i++){
					JSONObject obj = clists.getJSONObject(i);
					DictDistrictInfo mode = new DictDistrictInfo();
					mode.setDistrictid(obj.getString("district_id"));
					mode.setDistrictname(obj.getString("district_name"));
					lists.add(mode);
				}
				onResponseSuccess(lists);
				break;
			case 2:
				onOffline();
				break;
			case 4:
				onNoMoreData();
				break;

			default:
				onResponseFailed();
				break;
			}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			onResponseFailed();
		}
	}
	
	@Override
	public void onFailure(int statusCode, Header[] headers,
			String responseString, Throwable throwable) {
		super.onFailure(statusCode, headers, responseString, throwable);
		onResponseFailed();
	}
}
